import Vue from "vue";
import { checkPermision } from "~/utils/permision";
import Setting from "~/utils/setting";
const project = require("$project");

export default function({ app, $axios }) {
  Vue.prototype.$_has = function(permisionKey) {
    if (
      checkPermision(app, permisionKey, "widgets") ||
      checkPermision(app, permisionKey, "routers")
    ) {
      return true;
    } else {
      return false;
    }
  };

  //全局接口语言转换   
  Vue.prototype.$language = function(methodOptions) {
    let data = "";
    if (methodOptions) {
      switch (Setting.language) {
        case "zh":
          data = methodOptions.zh;
          break;
        case "en":
          data = methodOptions.en;
          break;
        case "mn":
          data = methodOptions.mn;
          break;
      }
    }
    return data;
  };
  //接口请求
  Vue.prototype.$http = {
    get(url: string, params: Object) {
      return common($axios, "GET", url, params);
    },
    delete(url: string, data: Object) {
      return common($axios, "DELETE", url, data);
    },
    put(url: string, data: Object) {
      return common($axios, "PUT", url, data);
    },
    post(url: string, data: Object) {
      return common($axios, "POST", url, data);
    }
  };
}

//旧版智慧校园配置
Vue.prototype.$project = project;

function common($axios: any, method: string, url: string, data: Object) {
  const methodStatus = method === "GET";
  return new Promise((resolve, reject) => {
    if (methodStatus) {
      $axios({
        url: url,
        method,
        params: data
      }).then(res => {
        commonS(res, resolve, reject);
      });
    } else {
      $axios({
        url: url,
        method,
        data
      }).then(res => {
        commonS(res, resolve, reject);
      });
    }
  });
}
function commonS(res, resolve, reject) {
  if (res && res.data.code === 200) {
    resolve(res.data);
  } else {
    reject(1);
  }
}
